package com.example.chengzhengwei;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.github.mikephil.charting.charts.LineChart;
import com.github.mikephil.charting.components.Legend;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.LineData;
import com.github.mikephil.charting.data.LineDataSet;
import com.github.mikephil.charting.formatter.ValueFormatter;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import androidx.core.content.FileProvider;
import java.util.Locale;

public class ExportActivity extends AppCompatActivity {

    private LineChart exportChart;
    private RecyclerView dataTable;
    private Button btnExportPdf;
    private DatabaseHelper dbHelper;
    private List<RunData> allData = new ArrayList<>();
    private static final int STORAGE_PERMISSION_REQUEST = 1001;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_export);

        // 设置Toolbar返回按钮
        androidx.appcompat.widget.Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        if (getSupportActionBar() != null) {
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
            getSupportActionBar().setDisplayShowHomeEnabled(true);
        }

        // 初始化组件
        exportChart = findViewById(R.id.exportChart);
        dataTable = findViewById(R.id.dataTable);
        btnExportPdf = findViewById(R.id.btnExportPdf);
        dbHelper = new DatabaseHelper(this);

        // 设置RecyclerView
        dataTable.setLayoutManager(new LinearLayoutManager(this));

        // 设置导出按钮点击事件
        btnExportPdf.setOnClickListener(v -> {
            if (allData.isEmpty()) {
                Toast.makeText(this, "没有可导出的数据", Toast.LENGTH_SHORT).show();
                return;
            }
            // 检查存储权限
            if (ContextCompat.checkSelfPermission(this,
                    Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
                ActivityCompat.requestPermissions(this,
                        new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, STORAGE_PERMISSION_REQUEST);
            } else {
            }
        });
    }


}